import { readFileSync } from 'fs-extra';
import { join } from 'path';
import { IPSDEForm, IPSDEFormDetail, IPSDEFormGroupPanel, IPSDEFormItemEx, IPSDEFormPage, IPSDEFormTabPage, IPSDEFormTabPanel, IPSModelService, PSModelServiceImpl } from '@ibizlab/model';

type IModel = Record<string, any>;

const modelFolder = 'M:\\Workspace\\Project\\mis\\model';

function readModelJson(modelPath: string): IModel {
  const path = join(modelFolder, modelPath);
  const content = readFileSync(path, 'utf-8');
  return JSON.parse(content);
}

function filterFormEditorTypes(form: IPSDEForm): string[] {
  const pages = form.getPSDEFormPages() || [];
  const result = [];
  for (const page of pages) {
    const items = page.getPSDEFormDetails() || [];
    result.push(...deepFilterEditorType(items));
  }
  return result;
}

function deepFilterEditorType(items: IPSDEFormDetail[]): string[] {
  const result: string[] = [];
  for (const item of items) {
    switch (item.detailType) {
      case 'FORMPAGE':
        const page = item as IPSDEFormPage;
        result.push(...deepFilterEditorType(page.getPSDEFormDetails() || []));
        break;
      case 'TABPANEL':
        const panel = item as IPSDEFormTabPanel;
        result.push(...deepFilterEditorType(panel.getPSDEFormTabPages() || []));
        break;
      case 'TABPAGE':
        const tabPage = item as IPSDEFormTabPage;
        result.push(...deepFilterEditorType(tabPage.getPSDEFormDetails() || []));
        break;
      case 'GROUPPANEL':
        const groupPanel = item as IPSDEFormGroupPanel;
        result.push(...deepFilterEditorType(groupPanel.getPSDEFormDetails() || []));
        break;
      case 'FORMITEMEX':
        const formItemEx = item as IPSDEFormItemEx;
        if (formItemEx.psEditor?.editorType) {
          result.push(formItemEx.psEditor.editorType);
        }
        break;
      case 'FORMITEM':
        const formItem = item as IPSDEFormItemEx;
        if (formItem.psEditor?.editorType) {
          result.push(formItem.psEditor.editorType);
        }
        break;
    }
  }
  return result;
}

const service: IPSModelService = new PSModelServiceImpl(readModelJson);

const apps = service.getPSSystem().allPSApps || [];

const allViewTypes: Set<string> = new Set();

const allCtrlTypes: Set<string> = new Set();

const allEditorType: Set<string> = new Set();

for (let i = 0; i < apps.length; i++) {
  const app = apps[i];
  const views = app.allPSAppViews || [];
  for (let j = 0; j < views.length; j++) {
    const view = views[j];
    allViewTypes.add(view.viewType);
    const controls = view.psControls || [];
    for (let k = 0; k < controls.length; k++) {
      const control = controls[k];
      allCtrlTypes.add(control.controlType);
      if (control.controlType === 'FORM') {
        const ctrl = control as IPSDEForm;
        const editorTypes = filterFormEditorTypes(ctrl);
        for (let l = 0; l < editorTypes.length; l++) {
          const editorType = editorTypes[l];
          allEditorType.add(editorType);
        }
      }
    }
  }
}

console.log('使用到的视图类型:\n', Array.from(allViewTypes).join('\n'));
console.log('使用到的部件类型:\n', Array.from(allCtrlTypes).join('\n'));
console.log('使用到的编辑器类型:\n', Array.from(allEditorType).join('\n'));
